package com.ih.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.ih.mapper.SalaryMapper;
import com.ih.pojo.Salary;
import com.ih.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@Service
public class SalaryServiceImpl implements SalaryService {
    @Autowired
    private SalaryMapper salaryMapper;

    @Override
    public PageInfo<Salary> calcSalaryByCondition(String startDate, String endDate, Integer pageNum, Integer pageSize) {
        if (StrUtil.isEmpty(endDate)){
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            endDate=sdf.format(date);
        }
        /*if (StrUtil.isEmpty(endDate)){
            Date date = new Date(0);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            startDate=sdf.format(date);
        }
        System.out.println("startDate = " + startDate);*/
        List<Salary> list = salaryMapper.calcSalaryByCondition(startDate,endDate);
        //工资降序
        Collections.sort(list,(o1,o2)->{
            if (o1.getFinalSalary()>o2.getFinalSalary()){
                return -1;
            }else if (o1.getFinalSalary()<o2.getFinalSalary()){
                return 1;
            }else {
                return 0;
            }
        });
        return new PageInfo<>(list);
    }

    /**
     * 查询没送过水的
     *
     * @return
     */
    @Override
    public PageInfo<Salary> IsNullWorker(Integer pageNum,Integer pageSize) {
        List<Salary> list = salaryMapper.IsNullWorker();

        Collections.sort(list,(o1,o2)->{
            if (o1.getFinalSalary()>o2.getFinalSalary()){
                return -1;
            }else if (o1.getFinalSalary()<o2.getFinalSalary()){
                return 1;
            }else {
                return 0;
            }
        });
        return new PageInfo<>(list);
    }
}
